#!/bin/bash
# Wrapper for building freon extension into a package.
# Version: 0.0.1
# (C) Chris Dorman, 2020 GPLv3+


# work directories
tmpdir="`pwd`/pkg"
srcdir="`pwd`/pkgsrc"
rootdir="`pwd`/src/rootfs"
freondir="/freon"
pkgdir="`pwd`/pkgs"

if [ ! -d "$tmpdir" ]; then
  echo "Warning: tmp directory not found, creating."
  mkdir $tmpdir > /dev/null 2>&1
fi

if [ ! -d "$pkgdir" ]; then
  echo "Warning: tgz directory not found, creating."
  mkdir $pkgdir > /dev/null 2>&1
fi

case $1 in
	* ) echo "Building $1.tgz..."
		if [ -d $freondir ]; then
			rm -rf $freondir
		fi
		
		if [ -d $tmpdir ]; then
			rm -rf $tmpdir
		fi

		./build-extensions $1

		mkdir $tmpdir
		mkdir $freondir
		
		if [ -f "deps/${1}.exec" ]; then
			cp deps/$1.exec $tmpdir/execute.sh
		fi

		cp -a $freondir $tmpdir/.
		cd $tmpdir

        tar -czf $1.tgz *
        cp $1.tgz $pkgdir/$1.tgz
        cd ..

        rm -rf $tmpdir

		echo "Done!"
		;;
esac
